/**
 * 弹窗管理器
 */
export const useDialogManager = defineStore("dialogManager", () => {
  const activeModal = ref();
  const modals = ref();

  const register = (name: string) => {
    modals.value[name] = {
      visible: false,
    };
  };

  const open = (name: string) => {
    if (activeModal.value && activeModal.value !== name) {
      close(activeModal.value);
    }
    modals.value[name].visible = true;
    activeModal.value = name;
  };

  const close = (name: string) => {
    if (modals.value[name]) {
      modals.value[name].visible = false;
    }
    activeModal.value = null;
  };

  const closeAll = () => {
    Object.keys(modals.value).forEach((name) => {
      modals.value[name].visible = false;
    });
    activeModal.value = null;
  };

  return {
    register,
    open,
    close,
    closeAll,
  };
});
